package com.cn.web.controller.mc;

import com.alibaba.fastjson2.JSONObject;
import com.cn.bean.pojo.mc.K3Dy;
import com.cn.bean.pojo.mc.K3Fl;
import com.cn.bean.vo.mc.K3WhVo;
import com.cn.config.config.Msg;
import com.cn.config.config.Result;
import com.cn.service.server.mc.K3WhServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/MC")
public class K3WhController {
    private final K3WhServer k3WhServer;

    @Autowired
    public K3WhController(K3WhServer k3WhServer) {
        this.k3WhServer = k3WhServer;
    }

    @RequestMapping("/select/K3Wh")
    @ResponseBody
    public K3WhVo selectK3Wh(@RequestBody JSONObject jsonObject) {
        int page = jsonObject.getIntValue("page");
        int limit = jsonObject.getIntValue("limit");
        String k3dyId = jsonObject.getString("k3dyId");
        String k3dyMc = jsonObject.getString("k3dyMc");
        k3dyId = (k3dyId != null) ? k3dyId : "";
        k3dyMc = (k3dyMc != null) ? k3dyMc : "";
        K3WhVo k3WhVo = new K3WhVo();
        List<K3Fl> k3Fls = k3WhServer.selectK3FlAll();
        List<K3Dy> k3DyList = k3WhServer.selectK3DyAll(limit, page, k3dyId, k3dyMc);
        k3WhVo.setK3Fls(k3Fls);
        k3WhVo.setK3DyList(k3DyList);
        return k3WhVo;
    }

    //删除分类
    @RequestMapping("/delete/McFl")
    @ResponseBody
    public Result deleteMcFl(@RequestBody JSONObject jsonObject) {
        Integer k3flId = jsonObject.getInteger("k3flId");
        int i = k3WhServer.deleteK3flId(k3flId);
        if (i > 0) {
            return Result.success("200", "删除成功");

        }
        return Result.failure("500", "删除失败");
    }

    //  提交分类
    @RequestMapping("/update/McFl")
    @ResponseBody
    public Result updateMcFl(@RequestBody JSONObject jsonObject) {
        Integer k3flId = jsonObject.getInteger("k3flId");
        String k3flLb = jsonObject.getString("k3flLb");
        Integer k3flState = jsonObject.getInteger("k3flState");
        String k3fLBz = jsonObject.getString("k3fLBz");
        Integer parentCategory = jsonObject.getInteger("parentCategory");
//没有上级分类
        if (parentCategory == null) {
            K3Fl fl = k3WhServer.selectK3flId(k3flId);
            if (fl.getK3flLb().equals(k3flLb)) {
                int i = k3WhServer.upDateMcFl(k3flId, k3flLb, k3flState, k3fLBz, 1, 1);
                if (i > 0) {
                    return Result.success("200", "修改成功");

                }
                return Result.failure("500", "修改失败");
            }
//有修改分类名
            int result = k3WhServer.repeatK3flLbLevel(fl.getLevel(), k3flLb);
            if (result > 0) {
                return Result.failure("500", "分类名称在同一层级名称重复");
            }
            int i = k3WhServer.upDateMcFl(k3flId, k3flLb, k3flState, k3fLBz, 1, 1);
            if (i > 0) {
                return Result.success("200", "修改成功");

            }
            return Result.failure("500", "修改失败");
        }
//有上级分类
        K3Fl fl = k3WhServer.selectK3flId(parentCategory);
        K3Fl fl1 = k3WhServer.selectK3flId(k3flId);

        if (fl1.getK3flLb().equals(k3flLb)) {
            int i = k3WhServer.upDateMcFl(k3flId, k3flLb, k3flState, k3fLBz, fl.getLevel() + 1, fl.getK3flId());
            if (i > 0) {
                return Result.success("200", "修改成功");
            }
        }
// 修改分类名
        int result = k3WhServer.repeatK3flLbLevel(fl.getLevel() + 1, k3flLb);
        if (result > 0) {
            return Result.failure("500", "分类名称在同一层级名称重复");
        }
        int i = k3WhServer.upDateMcFl(k3flId, k3flLb, k3flState, k3fLBz, fl.getLevel() + 1, fl.getK3flId());
        if (i > 0) {
            return Result.success("200", "修改成功");
        }
        return Result.failure("500", "修改失败");

    }

    @RequestMapping("/insert/McFl")
    @ResponseBody
    public Result insertMcFl(@RequestBody JSONObject jsonObject) {

        String userName = jsonObject.getString("userName");
        String k3flLb = jsonObject.getString("k3flLb");
        Integer k3flState = jsonObject.getInteger("k3flState");
        String k3fLBz = jsonObject.getString("k3fLBz");
        Integer parentCategory = jsonObject.getInteger("parentCategory");
        int k3flId = k3WhServer.getAutoId() + 1;
        if (parentCategory == null) {
            int result = k3WhServer.repeatK3flLbLevel(1, k3flLb);
            if (result > 0) {
                return Result.failure("500", "分类名称在同一层级名称重复");
            }
            int i = k3WhServer.insertMcFl(k3flId, k3flLb, k3flState, k3fLBz, 1, 1);
            if (i > 0) {
                return Result.success("200", "提交成功");
            }
            return Result.failure("500", "提交失败");
        }
        K3Fl fl = k3WhServer.selectK3flId(parentCategory);
        int level = fl.getLevel() + 1;
        int parentBh = fl.getK3flId();
        int result = k3WhServer.repeatK3flLbLevel(level, k3flLb);
        if (result > 0) {
            return Result.failure("500", "分类名称在同一层级名称重复");
        }
        int i = k3WhServer.insertMcFl(k3flId, k3flLb, k3flState, k3fLBz, level, parentBh);
        if (i > 0) {
            return Result.success("200", "提交成功");
        }
        return Result.failure("500", "提交失败");
    }

    //   点击分类侧边栏触发
    @RequestMapping("/select/K3Wh/Aside")
    @ResponseBody
    public K3WhVo selectK3WhAside(@RequestBody JSONObject jsonObject) {
        int page = jsonObject.getIntValue("page");
        int limit = jsonObject.getIntValue("limit");
        String k3dyId = jsonObject.getString("k3dyId");
        String k3dyMc = jsonObject.getString("k3dyMc");
        int k3dyFlId = jsonObject.getInteger("k3dyFlId");
        k3dyId = (k3dyId != null) ? k3dyId : "";
        k3dyMc = (k3dyMc != null) ? k3dyMc : "";
        K3WhVo k3WhVo = new K3WhVo();
        if (k3dyFlId == 1) {
            K3Fl fl = k3WhServer.selectK3flId(k3dyFlId);
            List<K3Dy> k3DyList = k3WhServer.selectK3DyAll(limit, page, k3dyId, k3dyMc);
            k3WhVo.setK3Fl(fl);
            k3WhVo.setK3DyList(k3DyList);
            return k3WhVo;
        }

        K3Fl fl = k3WhServer.selectK3flId(k3dyFlId);
        List<K3Fl> k3Fls = k3WhServer.selectK3FlAll();
        List<K3Dy> k3DyList = k3WhServer.selectK3DyAll(limit, page, k3dyId, k3dyMc);

        // 获取当前分类及其子分类
        List<K3Fl> subcategories = getSubcategories(k3Fls, k3dyFlId);

        // 获取当前分类及其子分类对应的 K3Dy
        List<K3Dy> categorizedDys = getCategorizedDys(k3DyList, subcategories);

        k3WhVo.setK3Fl(fl);
        k3WhVo.setK3DyList(categorizedDys);
        return k3WhVo;
    }


    private List<K3Fl> getSubcategories(List<K3Fl> allCategories, int parentFlId) {
        List<K3Fl> subcategories = new ArrayList<>();

        for (K3Fl category : allCategories) {
            if (category.getParentBh() == parentFlId) {
                subcategories.add(category);
                List<K3Fl> children = getSubcategories(allCategories, category.getK3flId());
                subcategories.addAll(children);
            } else if (category.getK3flId() == parentFlId) {
                // 如果当前分类就是目标分类，也将其添加到列表中
                subcategories.add(category);
            }
        }


        return subcategories;
    }

    private List<K3Dy> getCategorizedDys(List<K3Dy> allDys, List<K3Fl> categories) {
        List<K3Dy> categorizedDys = new ArrayList<>();

        for (K3Dy dy : allDys) {
            for (K3Fl category : categories) {
                if (dy.getK3dyFlId() == category.getK3flId()) {
                    categorizedDys.add(dy);
                    break;  // 如果匹配到当前分类，就不需要再继续查找
                }
            }
        }

        return categorizedDys;
    }

}
